package com.niodata.dt.fs;

import java.util.List;

public class ArrayUtil extends org.apache.commons.lang3.ArrayUtils {

  /**
   * is sa2 start with sa1.
   *
   * @param sa1 array1
   * @param sa2 array2
   * @return true/false
   */
  public static boolean startWith(String[] sa1, String[] sa2) {
    if (sa1.length > sa2.length) {
      return false;
    }
    int idx = 0;
    while (idx < sa1.length) {
      if (sa1[idx] == null && sa2[idx] == null) {
        idx++;
        continue;
      }
      if (sa1[idx] != null && sa2[idx] != null
            && sa1[idx].equals(sa2[idx])) {
        idx++;
        continue;
      }
      break;
    }
    return idx >= sa1.length;
  }

  /**
   * compare item one by one.
   * @param array1 object array1
   * @param array2 object array2
   * @return equals
   */
  public static boolean equals(Object[] array1, Object[] array2) {
    if (array1.length != array2.length) {
      return false;
    }
    for (int i = 0; i < array1.length; i++) {
      Object v1 = array1[i];
      Object v2 = array2[i];
      if (v1 == null && v2 == null) {
        continue;
      }
      if (v1 == null && v2 != null) {
        return false;
      }

      if (v1 != null && v2 == null) {
        return false;
      }
      if (!v1.equals(v2)) {
        return false;
      }
    }
    return true;
  }

  /**
   * compare item one by one.
   * @param list1 object list 1
   * @param list2 object list 2
   * @return equals
   */
  public static boolean equals(List list1, List list2) {
    if (list1.size() != list2.size()) {
      return false;
    }
    for (int i = 0; i < list1.size(); i++) {
      Object v1 = list1.get(i);
      Object v2 = list2.get(i);
      if (v1 == null && v2 == null) {
        continue;
      }
      if (v1 == null && v2 != null) {
        return false;
      }

      if (v1 != null && v2 == null) {
        return false;
      }
      if (!v1.equals(v2)) {
        return false;
      }
    }
    return true;
  }
}
